<template>
  <div class="cart-main">
    <div class="header">
      <div class="contentList">
        <div class="contentItem" v-for="item in contentList" :key="item.id">
          <router-link :to="'/videoDetail/' + item.videoId">
            <div>
              <el-image
                :src="item.coverUrl"
                style="width: 195px; height: 130px; border-radius: 6px"
                fit="fill"
              ></el-image>
            </div>
            <div class="text">{{ formatTitle(item.videoTitle) }}</div>
          </router-link>

          <div class="footer">
            <div class="text">收藏于：{{ dateFormat(item.gmtCreate) }}</div>
            <div @click="cancelCollect(item.videoId)">
              <svg
                t="1653032793464"
                class="icon"
                viewBox="0 0 1024 1024"
                version="1.1"
                xmlns="http://www.w3.org/2000/svg"
                p-id="3822"
                width="32"
                height="32"
              >
                <path
                  d="M795.3 469.1c14.7-14.3 6.6-39.2-13.7-42.1l-158.1-23c-8-1.2-15-6.2-18.6-13.5l-70.7-143.3c-9.1-18.4-35.2-18.4-44.3 0l-70.7 143.3c-3.6 7.3-10.6 12.3-18.6 13.5l-158.1 23c-20.3 2.9-28.3 27.8-13.7 42.1l114.4 111.5c5.8 5.7 8.5 13.8 7.1 21.9l-27 157.5c-3.5 20.2 17.7 35.6 35.8 26l141.5-74.4c7.2-3.8 15.8-3.8 23 0L664.9 786c18.1 9.5 39.3-5.9 35.8-26l-27-157.5c-1.4-8 1.3-16.2 7.1-21.9l114.5-111.5z m-170.5 96.3c-3.9 3.8-5.7 9.4-4.8 14.8l18.3 106.7c2.3 13.7-12 24.1-24.3 17.6l-95.8-50.4c-4.9-2.6-10.7-2.6-15.6 0l-95.8 50.4c-12.3 6.5-26.6-4-24.3-17.6l18.3-106.7c0.9-5.4-0.9-11-4.8-14.8l-77.5-75.6c-9.9-9.7-4.5-26.6 9.3-28.5L435 445.7c5.5-0.8 10.2-4.2 12.6-9.2l47.9-97.1c6.1-12.4 23.9-12.4 30 0l47.9 97.1c2.4 4.9 7.1 8.4 12.6 9.2l107 15.6c13.7 2 19.2 18.9 9.3 28.5l-77.5 75.6z"
                  fill="#1296db"
                  p-id="3823"
                ></path>
              </svg>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div>
      <el-pagination
        v-model:currentPage="page.currentPage"
        v-model:page-size="page.pageSize"
        :pager-count="page.pageCount"
        :total="page.total"
        @current-change="currentChange"
        small
        background
        layout="prev, pager, next"
      />
    </div>
  </div>
</template>

<script>
import VideoApi from '@/api/core/VideoDeatil'
import UserApi from '@/api/core/User'
import moment from 'moment'
export default {
  name: 'UserCart',
  data() {
    return {
      page: {
        currentPage: 1, //当前页数
        pageSize: 8, //每页记录数
        total: 0, //总记录数
        pageCount: 7, //每页折叠数
      },

      contentList: [],
    }
  },

  created() {
    this.getCollect()
  },

  methods: {
    getCollect(currentPage = 1, pageSize = 8) {
      UserApi.getCollect(currentPage, pageSize)
        .then((response) => {
          const pageInfo = response.data.collectionRecordsDetailPage

          this.contentList = pageInfo.records

          this.page.total = pageInfo.total
          this.page.currentPage = pageInfo.current
          this.page.pageSize = pageInfo.size
        })
        .catch((error) => {
          this.$message.error(error.message)
        })
    },

    currentChange() {
      this.getCollect(this.page.currentPage, this.page.pageSize)
    },

    cancelCollect(id) {
      this.$confirm('确认取消收藏？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        
        VideoApi.cancelCollect(id)
          .then(() => {
            this.getCollect(this.page.currentPage)
          })
          .catch((error) => {
            this.$message.error(error.message)
          })
      })
    },

    //格式话标题
    formatTitle(text) {
      if (text.length < 31) {
        return text
      }
      return text.substring(0, 31) + ' ...'
    },

    //格式化时间
    dateFormat(data) {
      return moment(data).format('YYYY-MM-DD')
    },
  },
}
</script>

<style scoped>
.cart-main {
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;
}
.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.header .contentList {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
}
.header .contentList .contentItem {
  width: 25%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10px;
}
.header .contentList .contentItem .text {
  width: 200px;
  height: 24px;
  font-size: 13px;
  color: black;
  white-space: pre-line;
}
.header .contentList .contentItem .footer {
  width: 200px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin-top: 3px;
}
.header .contentList .contentItem .footer .text {
  margin-top: 12px;
  height: 30px;
  font-size: 10px;
  color: #939393;
}
a {
  text-decoration: none;
}
</style>
